component {

	private struct function constructArguments(string from, string to){
		var local = {};
		local.args = {};
		local.args.beginDate_mm = dateformat(arguments.from,'mm');
		local.args.beginDate_dd = dateformat(arguments.from,'dd');
		local.args.beginDate_yyyy = dateformat(arguments.from,'yyyy');
		local.args.endDate_mm = dateformat(arguments.to,'mm');
		local.args.endDate_dd = dateformat(arguments.to,'dd');
		local.args.endDate_yyyy = dateformat(arguments.to,'yyyy');

		return local.args;
	}

	private any function search(string from, string to){
		var local={};

		//persist range
		if(structKeyExists(arguments,'from'))
			session.from = arguments.from;
		else
			arguments.from = session.from;
		if(structKeyExists(arguments,'to'))
			session.to = arguments.to;
		else
			arguments.to = session.to;

		//get data
		local.http = new http();
		local.params = constructArguments(argumentCollection=arguments);
		local.url='http://www.ncdc.noaa.gov/stormevents/csv?beginDate_mm=#local.params.beginDate_mm#&beginDate_dd=#local.params.beginDate_dd#&beginDate_yyyy=#local.params.beginDate_yyyy#&endDate_mm=#local.params.endDate_mm#&endDate_dd=#local.params.endDate_dd#&endDate_yyyy=#local.params.endDate_yyyy#&eventType=(C)+Flood&submitbutton=Search&statefips=-999%2CALLL';
		local.http.setMethod("get");
		local.http.setAttributes(name='name',value='q');
		local.http.setURL(local.url);			
		local.data = local.http.send().getResult();

		return local.data;
	}

	
	public any function list(string from, string to) {
		var local={};

		var data = search(argumentCollection=arguments);

		//sort data
		local.sortedData1 = new query();
		local.sortedData1.setName("dataQuery1");
		local.sortedData1.setDBType("query");
		local.sortedData1.setAttributes(sourceQuery=data);
		local.sortedDataSQL1 = "SELECT begin_date,injuries_direct,state_abbr,cast(damage_property_num as integer) as damage_property_num FROM sourceQuery order by damage_property_num desc";
		local.sortedDataResult1 = local.sortedData1.execute(sql = local.sortedDataSQL1).getresult();
		local.result.1 = local.sortedDataResult1;

		//sort data
		local.sortedData2 = new query();
		local.sortedData2.setName("dataQuery2");
		local.sortedData2.setDBType("query");
		local.sortedData2.setAttributes(sourceQuery=data);
		local.sortedDataSQL2 = "SELECT * FROM sourceQuery order by injuries_direct desc";
		local.sortedDataResult2 = local.sortedData2.execute(sql = local.sortedDataSQL2).getresult();
		local.result.2 = local.sortedDataResult2;
		
		return local.result;
	}



	public any function chart(string from, string to) {
		var local={};

		var data = search(argumentCollection=arguments);

		//group data
		local.groupedData1 = new query();
		local.groupedData1.setName("dataQuery1");
		local.groupedData1.setDBType("query");
		local.groupedData1.setAttributes(sourceQuery=data);
		local.groupedDataSQL1 = "SELECT cast(damage_property_num as integer) as category, count(1) as num FROM sourceQuery group by damage_property_num order by category";
		local.groupedDataResult1 = local.groupedData1.execute(sql = local.groupedDataSQL1).getresult();
		local.result.1 = local.groupedDataResult1;

		//group data
		local.groupedData2 = new query();
		local.groupedData2.setName("dataQuery2");
		local.groupedData2.setDBType("query");
		local.groupedData2.setAttributes(sourceQuery=data);
		local.groupedDataSQL2 = "SELECT State_abbr as category, count(1) as num FROM sourceQuery group by State_abbr order by num desc";
		local.groupedDataResult2 = local.groupedData2.execute(sql = local.groupedDataSQL2).getresult();
		local.result.2 = local.groupedDataResult2;

		return local.result;
	}

	public any function map(string from, string to) {
		var local={};

		var data = search(argumentCollection=arguments);

		//sort data
		local.sortedData = new query();
		local.sortedData.setName("dataQuery");
		local.sortedData.setDBType("query");
		local.sortedData.setAttributes(sourceQuery=data);
		local.sortedDataSQL = "SELECT cast(damage_property_num as integer) as category,* FROM sourceQuery order by category desc";
		local.result.data = local.sortedData.execute(sql = local.sortedDataSQL).getresult();
		local.result.coordinates=Coordinates();
		
		return local.result;
	}

	public struct function Coordinates(){
		var local={};
		local.coordinates={
			AK={lat=61.3850,lon=-152.2683},
			AL={lat=32.7990,lon=-86.8073},
			AR={lat=34.9513,lon=-92.3809},
			AS={lat=14.2417,lon=-170.7197},
			AZ={lat=33.7712,lon=-111.3877},
			CA={lat=36.1700,lon=-119.7462},
			CO={lat=39.0646,lon=-105.3272},
			CT={lat=41.5834,lon=-72.7622},
			DC={lat=38.8964,lon=-77.0262},
			DE={lat=39.3498,lon=-75.5148},
			FL={lat=27.8333,lon=-81.7170},
			GA={lat=32.9866,lon=-83.6487},
			HI={lat=21.1098,lon=-157.5311},
			IA={lat=42.0046,lon=-93.2140},
			ID={lat=44.2394,lon=-114.5103},
			IL={lat=40.3363,lon=-89.0022},
			'IN'={lat=39.8647,lon=-86.2604},
			KS={lat=38.5111,lon=-96.8005},
			KY={lat=37.6690,lon=-84.6514},
			LA={lat=31.1801,lon=-91.8749},
			MA={lat=42.2373,lon=-71.5314},
			MD={lat=39.0724,lon=-76.7902},
			ME={lat=44.6074,lon=-69.3977},
			MI={lat=43.3504,lon=-84.5603},
			MN={lat=45.7326,lon=-93.9196},
			MO={lat=38.4623,lon=-92.3020},
			MP={lat=14.8058,lon=145.5505},
			MT={lat=46.9048,lon=-110.3261},
			NC={lat=35.6411,lon=-79.8431},
			ND={lat=47.5362,lon=-99.7930},
			NE={lat=41.1289,lon=-98.2883},
			NH={lat=43.4108,lon=-71.5653},
			NJ={lat=40.3140,lon=-74.5089},
			NM={lat=34.8375,lon=-106.2371},
			NV={lat=38.4199,lon=-117.1219},
			NY={lat=42.1497,lon=-74.9384},
			OH={lat=40.3736,lon=-82.7755},
			OK={lat=35.5376,lon=-96.9247},
			'OR'={lat=44.5672,lon=-122.1269},
			PA={lat=40.5773,lon=-77.2640},
			PR={lat=18.2766,lon=-66.3350},
			RI={lat=41.6772,lon=-71.5101},
			SC={lat=33.8191,lon=-80.9066},
			SD={lat=44.2853,lon=-99.4632},
			TN={lat=35.7449,lon=-86.7489},
			TX={lat=31.1060,lon=-97.6475},
			UT={lat=40.1135,lon=-111.8535},
			VA={lat=37.7680,lon=-78.2057},
			VI={lat=18.0001,lon=-64.8199},
			VT={lat=44.0407,lon=-72.7093},
			WA={lat=47.3917,lon=-121.5708},
			WI={lat=44.2563,lon=-89.6385},
			WV={lat=38.4680,lon=-80.9696},
			WY={lat=42.7475,lon=-107.2085}
		};
		return local.coordinates;
	}

}
